//系统配置-用户管理
App.pages.add((function(){
    var NS = Admin.Access.User;
    var page = new App.Page({
        name: 'access/user/index'
    });
    //页面初始化
    page.initStack.add('dataTable', function(){
        //面包屑导航
        this.setBreadcrumb([{
            icon: '<i class="fa fa-folder"></i>',
            content: '系统配置'
        }, {
            content: '用户管理'
        }]);
        this.dataTable = new App.DataTable({
            baseUrl: '/admin/access/user',
            dataUrl: 'lists',
            table: {
                id: 'user_id',
                main: 0,
                columns: App.util.copy(NS.Conf.columns)
            },
            filter: [
                {type: 'keywords'},
                {type: 'timeRange'},
                {type: 'select', name: 'status', title: '用户状态', width: '100px', items: [
                    ['1', '开启'],
                    ['0', '关闭']
                ]}
            ],
            menu: [
                {parent: 'add', permission: 'op:admin.access.user.create'},
                {parent: 'view', permission: 'op:admin.access.user.detail'},
                {parent: 'edit', permission: 'op:admin.access.user.update'},
                {type: 'inline', permission: 'op:admin.access.user.open', name: '开启', operation: {type: 'change', url: 'open'}, conditions: [['status', '0']]},
                {type: 'inline', permission: 'op:admin.access.user.close', name: '关闭', operation: {type: 'change', url: 'close'}, conditions: [['status', '1']]},
                {parent: 'drop', permission: 'op:admin.access.user.delete'},
                {type: 'inline', permission: 'op:admin.access.user.resetPassword', name: '重置密码', operation: function(dataTable, user){
                    var win = dataTable.wins.get('resetPassword');
                    win.form.change({password: '', re_password: '', user_id: user.user_id});
                    win.setTitle('重置密码 - '+user.nickname+'（'+user.username+'）');
                    win.show();
                }},
                {type: 'inline', permission: 'op:admin.access.user.changeRole', name: '角色管理', operation: function(dataTable, user){
                    var win = dataTable.wins.get('roleManage');
                    //填充内容
                    App.util.request.getData({
                        url: dataTable.url('getWithRole'),
                        data: {user_id: user.user_id},
                        success: function(data){
                            var selected = data.user.role_id === null ? 0 : data.user.role_id;
                            var roles = data.roles;
                            win.setTitle('角色管理 - '+user.nickname+'（'+user.username+'）');
                            win.contentContainer.find('input[name="user_id"]').val(user.user_id);
                            win.printer.flush(roles, selected);
                            win.show();
                        }
                    });
                }},
                {type: 'multi', name: '状态', openable: true, items: [
                    {type: 'multi', permission: 'op:admin.access.user.multiOpen', name: '开启', operation: {type: 'change', url: 'multiOpen', confirm: true}},
                    {type: 'multi', permission: 'op:admin.access.user.multiClose', name: '关闭', operation: {type: 'change', url: 'multiClose', confirm: true}}
                    ], permission: [
                        'op:admin.access.user.multiOpen',
                        'op:admin.access.user.multiClose'
                ]},
                {parent: 'multiDrop', permission: 'op:admin.access.user.multiDelete'},
                {parent: 'oplog', permission: 'op:admin.access.user.getOpLogs'}
            ],
            detail: {
                form: {
                    items: App.util.copy(NS.Conf.form),
                    add: 'all',
                    edit: 'except:password,re_password',
                    view: 'except:password,re_password'
                }
            },
            after: function(dataTable){
                dataTable.wins.add(new NS.ResetPassword(dataTable), 'resetPassword');
                dataTable.wins.add(new NS.RoleManage(dataTable), 'roleManage');
            }
        }, page);
        this.dataTable.fix(this.contentContainer);
        this.dataTable.loadData();
    });
    //页面刷新
    page.refreshStack.add('dataTable', function(){
        this.dataTable.loadData(true);
    });
    //页面销毁
    page.destroyStack.add('dataTable', function(){
        this.dataTable.destroy();
    });
    return page;
})());